﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RainyPWS.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RDropDownList runat=\"server\"></{0}:RDropDownList>")]
    public class RDropDownList : DropDownList
    {
        private const int TriggerWidth = 17;
        private const int LableMarginWidth = 5;
        private const int PaddingBorderWidth = 8;
        private const int DefaultLabelWidth = 80;
        private const int DefaultSelectWidth = 250;

        [Bindable(true)]
        public string Caption
        {
            get
            {
                String s = (String)ViewState["Caption"];
                return ((s == null) ? "Caption" : s);
            }
            set
            {
                ViewState["Caption"] = value;
            }
        }

        /// <summary>
        /// label宽度
        /// </summary>
        public Unit CaptionWidth
        {
            get
            {
                if (ViewState["CaptionWidth"] != null)
                {
                    return (Unit)ViewState["CaptionWidth"];
                }
                else
                {
                    return new Unit(DefaultLabelWidth);
                }
            }
            set
            {
                if (value.Value > TotalWidth.Value - Width.Value)
                {
                    value = new Unit(TotalWidth.Value - Width.Value);
                }
                ViewState["CaptionWidth"] = value;

            }
        }

        public Unit TotalWidth
        {
            get
            {
                if (ViewState["TotalWidth"] != null)
                {
                    return (Unit)ViewState["TotalWidth"];
                }
                else
                {
                    return new Unit(DefaultLabelWidth + DefaultSelectWidth);
                }
            }
            set
            {
                if (value.Value < Width.Value + CaptionWidth.Value)
                {
                    value = new Unit(Width.Value + CaptionWidth.Value);
                }
                ViewState["TotalWidth"] = value;
            }
        }

        public override Unit Width
        {
            get
            {
                Unit BorderPaddingWidth;
                if (ViewState["BorderPaddingWidth"] != null)
                {
                    BorderPaddingWidth = (Unit)ViewState["BorderPaddingWidth"];
                }
                else
                {
                    BorderPaddingWidth = new Unit(DefaultSelectWidth);
                    ViewState["BorderPaddingWidth"] = BorderPaddingWidth;
                }
                return BorderPaddingWidth;
            }
            set
            {
                if (value.Value > TotalWidth.Value - CaptionWidth.Value)
                {
                    value = new Unit(TotalWidth.Value - CaptionWidth.Value);
                }
                ViewState["BorderPaddingWidth"] = value;
            }
        }

        public string Text
        {
            get
            {
                string s = (string)ViewState["Text"];
                return (s == null) ? string.Empty : s;
            }
            set
            {
                value = value.Trim();
                ViewState["Text"] = value;
                foreach (ListItem li in this.Items)
                {
                    if (li.Text.Trim() == value)
                    {
                        Value = li.Value;
                    }
                }
            }
        }

        public string Value
        {
            get
            {
                string s = (string)ViewState["BackValue"];
                return (s == null) ? string.Empty : s;
            }
            set
            {
                value = value.Trim();
                ViewState["BackValue"] = value;
            }
        }

        protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            string text = this.Text;
            string str2 = postCollection[postDataKey];
            if (!text.Equals(str2, StringComparison.Ordinal))
            {
                this.Text = str2;
                return true;
            }
            return false;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (Text == string.Empty && this.SelectedItem != null)
            {   
                Text = this.SelectedItem.Text;
            }
            writer.Write("<div class=\"d-form-item\" style=\"width:" + TotalWidth.Value + "px;\">");
            writer.Write("<label for=\"\" class=\"d-form-item-label\" style=\"width:" + (CaptionWidth.Value - LableMarginWidth) + "px; margin-right:" + LableMarginWidth + "px;\">" + Caption + ":</label>");
            writer.Write("<div class=\"d-form-item-body\" style=\"width:" + Width.Value + "px;\">");
            writer.Write("<input type=\"text\" readonly=\"true\" id=\"" + this.ClientID + "\" name=\"" + this.UniqueID + "\" class=\"d-form-field d-form-text\" style=\"width:" + (Width.Value - TriggerWidth - PaddingBorderWidth) + "px;\" value=\"" + Text + "\" />");
            writer.Write("<div class=\"d-form-trigger-wrap\" style=\"width:" + TriggerWidth + "px;\">");
            writer.Write("<div class=\"d-form-trigger\" id=\"" + this.ClientID + "_btn\"></div>");
            writer.Write("<div class=\"d-clear\"></div>");
            writer.Write("</div>");
            writer.Write("</div>");
            writer.Write("<div class=\"d-clear\"></div>");
            writer.Write("</div>");
            ListItemCollection items = this.Items;
            int count = items.Count;
            string HeightStyle = "";
            if (count > 12)
            {
                HeightStyle = "height:298px;";
            }
            writer.Write("<div id=\"" + this.ClientID + "_list\" class=\"d-layer d-boundlist\" style=\"display:none; width:" + (Width.Value - 2) + "px; z-index:190000; " + HeightStyle + "\">");
            writer.Write("<div style=\"overflow:auto; " + HeightStyle + "\">");
            writer.Write("<ul>");
            for (int i = 0; i < count; i++)
            {
                ListItem item = items[i];
                if (item.Enabled)
                {
                    writer.WriteBeginTag("li");
                    writer.WriteAttribute("id", this.ClientID + "_list_" + i);
                    if (item.Selected)
                    {
                        writer.WriteAttribute("class", "d-boundlist-item-selected");
                    }
                    else
                    {
                        writer.WriteAttribute("class", "d-boundlist-item");
                    }
                    writer.Write('>');
                    HttpUtility.HtmlEncode(item.Text, writer);
                    writer.WriteEndTag("li");
                }
            }
            writer.Write("</ul>");
            writer.Write("</div>");
            writer.Write("</div>");
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "RainyPWS_RDropDownList"))
            {
                string jsURL = Page.ClientScript.GetWebResourceUrl(this.GetType(), "RainyPWS.Controls.source.RainyPWS_RDropDownList.js");
                Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "RainyPWS_RDropDownList", jsURL);
            }

            StringBuilder jsInit = new StringBuilder();
            jsInit.AppendLine("$(document).ready(function(){");
            jsInit.AppendLine("\t$(\"#" + this.ClientID + "\").ddlInit();");
            jsInit.AppendLine("});");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.UniqueID, jsInit.ToString(), true);
        }
    }
}
